﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SimWithP3d
{
    public class NotifyChanged : INotifyPropertyChanged, INotifyCollectionChanged, INotifyDataErrorInfo
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
        public void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }


        public bool HasErrors { get { return Errors.Count > 0; } }

        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
        private Dictionary<string, ObservableCollection<string>> errors;

        public Dictionary<string, ObservableCollection<string>> Errors
        {
            get
            {
                if (errors == null)
                {
                    errors = new Dictionary<string, ObservableCollection<string>>();
                }
                return errors;
            }
            set { errors = value; OnPropertyChanged(new PropertyChangedEventArgs("Errors")); }
        }

        public IEnumerable GetErrors(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return Errors.Values;
            }
            else
            {
                if (Errors.ContainsKey(propertyName))
                {
                    return Errors[propertyName];
                }
                else
                {
                    return null;
                }

            }
        }
        public void SetErrors(string propertyName, ObservableCollection<string> propertyErrors)
        {
            Errors.Remove(propertyName);

            Errors.Add(propertyName, propertyErrors);
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }
        public void ClearErrors(string propertyName)
        {
            Errors.Remove(propertyName);
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }
    }
    /// <summary>
    /// Simplistic delegate command for the demo.
    /// </summary>
    public class DelegateCommand : ICommand
    {
        public Action CommandAction { get; set; }
        public Func<bool> CanExecuteFunc { get; set; }

        public void Execute(object parameter)
        {
            CommandAction();
        }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null || CanExecuteFunc();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
